package com.wsj.util.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
	/**
	 * ��ʽ������
	 * 
	 * @param dateStr
	 *            �ַ�������
	 * @param format
	 *            ��ʽ
	 * @return ��������
	 */

	public static java.util.Date parseDate(java.sql.Date date) {
		return date;
	}

	public static java.sql.Date parseSqlDate(java.util.Date date) {
		if (date != null)
			return new java.sql.Date(date.getTime());
		else
			return null;
	}

	/**
	 *  将字符的日期转换成 util.date
	 * @author gbx
	 * 
	 * @param format yyyy-MM-dd HH:mm:ss
	 *            字符格式的日期
	 * @return util.date
	 */
	public static java.util.Date parseDate(String format) {
		try {
			if (format != null) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				return sdf.parse(format);
			}
		} catch (Exception e) {
		}
		return null;
	}
	
	
	/**
	 * ��ʽ���������
	 * 
	 * @param date
	 *            ����
	 * @param format
	 *            ��ʽ
	 * @return �����ַ�������
	 */
	public static String format(java.util.Date date, String format) {
		String result = "";
		try {
			if (date != null) {
				java.text.DateFormat df = new java.text.SimpleDateFormat(format);
				result = df.format(date);
			}
		} catch (Exception e) {
		}
		return result;
	}

	public static String format(java.util.Date date) {
		return format(date, "yyyy/MM/dd");
	}

	/**
	 * �������
	 * 
	 * @param date
	 *            ����
	 * @return �������
	 */
	public static int getYear(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.YEAR);
	}

	/**
	 * �����·�
	 * 
	 * @param date
	 *            ����
	 * @return �����·�
	 */
	public static int getMonth(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.MONTH) + 1;
	}

	/**
	 * �����շ�
	 * 
	 * @param date
	 *            ����
	 * @return �����շ�
	 */
	public static int getDay(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.DAY_OF_MONTH);
	}

	/**
	 * ����Сʱ
	 * 
	 * @param date
	 *            ����
	 * @return ����Сʱ
	 */
	public static int getHour(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.HOUR_OF_DAY);
	}

	/**
	 * ���ط���
	 * 
	 * @param date
	 *            ����
	 * @return ���ط���
	 */
	public static int getMinute(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.MINUTE);
	}

	/**
	 * ��������
	 * 
	 * @param date
	 *            ����
	 * @return ��������
	 */
	public static int getSecond(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.SECOND);
	}

	/**
	 * ���غ���
	 * 
	 * @param date
	 *            ����
	 * @return ���غ���
	 */
	public static long getMillis(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	/**
	 * �����ַ�������
	 * 
	 * @param date
	 *            ����
	 * @return �����ַ������� yyyy-MM-dd
	 */
	public static String getDate(java.util.Date date) {
		return format(date, "yyyy-MM-dd");
	}

	/**
	 * �����ַ���ʱ��
	 * 
	 * @param date
	 *            ����
	 * @return �����ַ���ʱ�� HH:mm:ss
	 */
	public static String getTime(java.util.Date date) {
		return format(date, "HH:mm:ss");
	}

	/**
	 * �����ַ�������ʱ��
	 * 
	 * @param date
	 *            ����
	 * @return �����ַ�������ʱ�� yyyy-MM-dd HH:mm:ss
	 */
	public static String getDateTime(java.util.Date date) {
		return format(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * �������
	 * 
	 * @param date
	 *            ����
	 * @param day
	 *            ����
	 * @return ������Ӻ������
	 */
	public static java.util.Date addDate(java.util.Date date, int day) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	/**
	 * �������
	 * 
	 * @param date
	 *            ����
	 * @param date1
	 *            ����
	 * @return �������������
	 */
	public static int diffDate(java.util.Date date, java.util.Date date1) {
		return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
	}
	
	public static boolean isSameDay(java.util.Date date, java.util.Date date1) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(date);
		c2.setTime(date1);
		int c1_day = c1.get(Calendar.DAY_OF_YEAR);
		int c1_y = c1.get(Calendar.YEAR);

		int c2_day = c2.get(Calendar.DAY_OF_YEAR);
		int c2_y = c2.get(Calendar.YEAR);
		
		if(c1_day == c2_day && c1_y == c2_y) {
			return true;
		}else {
			return false;
		}
	}
	
	
	public static Date getDate(String datestr) {
		String format="";
		if (format == null || "".equals(format)) {
			if(datestr.length()<=10) {
				format="yyyy-MM-dd";
			}else {
				format="yyyy-MM-dd HH:mm:ss";
			}
		}
		SimpleDateFormat sd=new SimpleDateFormat(format);
		try {
			Date d = sd.parse(datestr);
			return d;
		} catch (ParseException e) {
			return null;
		}
		
	}
	
	/***
	 * 
	 * 计算相差月份
	 * */
	
	public static final int getMonths(GregorianCalendar g1, GregorianCalendar g2)    
    {    
        int elapsed = 0;    
        GregorianCalendar gc1, gc2;    
   
        if (g2.after(g1))    
        {    
            gc2 = (GregorianCalendar) g2.clone();    
            gc1 = (GregorianCalendar) g1.clone();    
        }    
        else   
        {    
            gc2 = (GregorianCalendar) g1.clone();    
            gc1 = (GregorianCalendar) g2.clone();    
        }    
   
        gc1.clear(Calendar.MILLISECOND);    
        gc1.clear(Calendar.SECOND);    
        gc1.clear(Calendar.MINUTE);    
        gc1.clear(Calendar.HOUR_OF_DAY);    
        gc1.clear(Calendar.DATE);    
   
        gc2.clear(Calendar.MILLISECOND);    
        gc2.clear(Calendar.SECOND);    
        gc2.clear(Calendar.MINUTE);    
        gc2.clear(Calendar.HOUR_OF_DAY);    
        gc2.clear(Calendar.DATE);    
   
        while (gc1.before(gc2))    
        {    
            gc1.add(Calendar.MONTH, 1);    
            elapsed++;    
        }    
        return elapsed;    
    }   
	
	
	
}
